package com.micro.ai.commons.client;

import com.micro.ai.commons.domain.Result;
import com.micro.ai.commons.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * HTTP 客户端工具类
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@Component
public class HttpClient {

    private final RestTemplate restTemplate;

    public HttpClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * GET 请求
     */
    public <T> Result<T> get(String url, Class<T> responseType) {
        return get(url, null, responseType);
    }

    /**
     * GET 请求带参数
     */
    public <T> Result<T> get(String url, Map<String, Object> params, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<String> entity = new HttpEntity<>(headers);
            
            ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
            
            if (response.getStatusCode().is2xxSuccessful()) {
                return Result.success(response.getBody());
            } else {
                return Result.error("请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            log.error("GET 请求失败: {}", url, e);
            return Result.error("请求失败: " + e.getMessage());
        }
    }

    /**
     * POST 请求
     */
    public <T> Result<T> post(String url, Object requestBody, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            String jsonBody = JsonUtils.toJson(requestBody);
            HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
            
            ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
            
            if (response.getStatusCode().is2xxSuccessful()) {
                return Result.success(response.getBody());
            } else {
                return Result.error("请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            log.error("POST 请求失败: {}", url, e);
            return Result.error("请求失败: " + e.getMessage());
        }
    }

    /**
     * PUT 请求
     */
    public <T> Result<T> put(String url, Object requestBody, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            String jsonBody = JsonUtils.toJson(requestBody);
            HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
            
            ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.PUT, entity, responseType);
            
            if (response.getStatusCode().is2xxSuccessful()) {
                return Result.success(response.getBody());
            } else {
                return Result.error("请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            log.error("PUT 请求失败: {}", url, e);
            return Result.error("请求失败: " + e.getMessage());
        }
    }

    /**
     * DELETE 请求
     */
    public <T> Result<T> delete(String url, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<String> entity = new HttpEntity<>(headers);
            
            ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.DELETE, entity, responseType);
            
            if (response.getStatusCode().is2xxSuccessful()) {
                return Result.success(response.getBody());
            } else {
                return Result.error("请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            log.error("DELETE 请求失败: {}", url, e);
            return Result.error("请求失败: " + e.getMessage());
        }
    }

    /**
     * 发送请求（通用方法）
     */
    public <T> Result<T> request(String url, HttpMethod method, Object requestBody, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            String jsonBody = requestBody != null ? JsonUtils.toJson(requestBody) : null;
            HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
            
            ResponseEntity<T> response = restTemplate.exchange(url, method, entity, responseType);
            
            if (response.getStatusCode().is2xxSuccessful()) {
                return Result.success(response.getBody());
            } else {
                return Result.error("请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            log.error("请求失败: {} {}", method, url, e);
            return Result.error("请求失败: " + e.getMessage());
        }
    }
}
